package com.ruoyi.web.controller.miniprogram;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.dto.SysHomepageResDto;
import com.ruoyi.common.core.domain.entity.miniprogram.DescAndIcon;
import com.ruoyi.common.core.domain.entity.system.SysResource;
import com.ruoyi.common.core.domain.vo.MiniScenicVo;
import com.ruoyi.common.oss.OssManager;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.system.service.ISysResourceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/miniprogram/sys")
public class MiniSystemController extends BaseController {

    @Autowired
    private ISysResourceService sysResourceService;

    @Autowired
    private OssManager ossManager;

    /**
     * 小程序支付倒计时时长,单位：秒。
     */
    @Value("${miniprogram.payCountdownDuration}")
    private int payCountdownDuration;

    /**
     * 小程序扫码确认上船倒计时时长,单位：秒。
     */
    @Value("${miniprogram.onBoatCountdownDuration}")
    private int onBoatCountdownDuration;

    /**
     * 小程序向后端发送websocket心跳包的间隔时长,单位：秒。
     */
    @Value("${miniprogram.wsHeartBeatDuration}")
    private int wsHeartBeatDuration;

    /**
     * 获取当前所有处于运营中状态的景区信息列表。
     *
     * @return
     */
    @GetMapping("/getScenicInfoList")
    public AjaxResult getScenicInfoList() {
        List<MiniScenicVo> scenicVos = sysResourceService.getScenicInfoList();
        return success(scenicVos);
    }

    public AjaxResult getBoatList() {

        return success();
    }

    /**
     * 获取平台系统资源对象。
     *
     * @return
     */
    @GetMapping("/getSysResource")
    public AjaxResult getSysResource() {
        SysResource sysResource = sysResourceService.getSysResourceByName();
        return success(sysResource);
    }

    /**
     * 获取小程序首页相关资源(含)。
     *
     * @return
     */
    @GetMapping("/getHomepageRes")
    public AjaxResult getHomepageRes() {
        SysHomepageResDto sysHomepageResDto = sysResourceService.getHomepageRes();
        return success(sysHomepageResDto);
    }

    /**
     * 更新首页轮播图。
     *
     * @param imageNameArr
     * @return
     */
    @PostMapping("/updateSlidePictures")
    public AjaxResult updateSlidePictures(@RequestBody String[] imageNameArr) {
        log.info("updateSlidePictures imageUrls: {}", Arrays.toString(imageNameArr));
        return toAjax(sysResourceService.updateSlidePictures(imageNameArr));
    }

    /**
     * 获取轮播图。该接口对用户是否登录无要求。
     *
     * @return
     */
    @GetMapping("/getSlidePictures")
    public AjaxResult getSlidePictures() {
        SysResource sysResource = sysResourceService.getSysResourceByName();
        List<String> slidePicNameList = new ArrayList<>();
        String slidePic1Name = sysResource.getSlidePic1Name();
        if (!StringUtils.isEmpty(slidePic1Name)) {
            URL url = ossManager.generatePresignedUrl(slidePic1Name);
            slidePicNameList.add(url.toString());
        }
        String slidePic2Name = sysResource.getSlidePic2Name();
        if (!StringUtils.isEmpty(slidePic2Name)) {
            URL url = ossManager.generatePresignedUrl(slidePic2Name);
            slidePicNameList.add(url.toString());
        }
        String slidePic3Name = sysResource.getSlidePic3Name();
        if (!StringUtils.isEmpty(slidePic3Name)) {
            URL url = ossManager.generatePresignedUrl(slidePic3Name);
            slidePicNameList.add(url.toString());
        }
        String slidePic4Name = sysResource.getSlidePic4Name();
        if (!StringUtils.isEmpty(slidePic4Name)) {
            URL url = ossManager.generatePresignedUrl(slidePic3Name);
            slidePicNameList.add(url.toString());
        }
        String slidePic5Name = sysResource.getSlidePic5Name();
        if (!StringUtils.isEmpty(slidePic5Name)) {
            URL url = ossManager.generatePresignedUrl(slidePic5Name);
            slidePicNameList.add(url.toString());
        }

        return success(slidePicNameList);
    }

    /**
     * 获取支付倒计时时长和上船倒计时时长。
     *
     * @return
     */
    @GetMapping("/getCountdownDuration")
    public AjaxResult getCountdownDuration() {
        AjaxResult ajaxResult = AjaxResult.success();
        ajaxResult.put("payCountdownDuration", payCountdownDuration);
        ajaxResult.put("onBoatCountdownDuration", onBoatCountdownDuration);
        ajaxResult.put("wsHeartBeatDuration", wsHeartBeatDuration);

        return ajaxResult;
    }

    /**
     * 更新乘船须知文件。
     *
     * @param boatingNoticeFilename
     * @return
     */
    @PostMapping("/updateBoatingNoticeFile")
    public AjaxResult updateBoatingNoticeFile(@RequestBody String boatingNoticeFilename) {
        return toAjax(sysResourceService.updateBoatingNoticeFile(boatingNoticeFilename));
    }

    /**
     * 更新乘船须知文本内容。
     *
     * @param boatingNoticeContent
     * @return
     */
    @PostMapping("/updateBoatingNoticeContent")
    public AjaxResult updateBoatingNoticeContent(@RequestBody String boatingNoticeContent) {
        return toAjax(sysResourceService.updateBoatingNoticeContent(boatingNoticeContent));
    }

    /**
     * 更新乘船指南文本内容。
     *
     * @param boatingGuideContent
     * @return
     */
    @PostMapping("/updateBoatingGuideContent")
    public AjaxResult updateBoatingGuideContent(@RequestBody String boatingGuideContent) {
        return toAjax(sysResourceService.updateBoatingGuideContent(boatingGuideContent));
    }

    /**
     * 更新关于我们文本内容。
     *
     * @param aboutWeContent
     * @return
     */
    @PostMapping("/updateAboutWe")
    public AjaxResult updateAboutWe(@RequestBody String aboutWeContent) {
        return toAjax(sysResourceService.updateAboutWe(aboutWeContent));
    }

    /**
     * 删除乘船须知文件。
     *
     * @return
     */
    @DeleteMapping("/deleteBoatingNoticeFile")
    public AjaxResult deleteBoatingNoticeFile() {
        return toAjax(sysResourceService.deleteBoatingNoticeFile());
    }

    /**
     * 更新乘船指南文件。
     *
     * @param boatingGuideFilename
     * @return
     */
    @PostMapping("/updateBoatingGuideFile")
    public AjaxResult updateBoatingGuideFile(@RequestBody String boatingGuideFilename) {
        return toAjax(sysResourceService.updateBoatingGuideFile(boatingGuideFilename));
    }

    /**
     * 删除乘船指南文件。
     *
     * @return
     */
    @DeleteMapping("/deleteBoatingGuideFile")
    public AjaxResult deleteBoatingGuideFile() {
        return toAjax(sysResourceService.deleteBoatingGuideFile());
    }


    @GetMapping("/getDocumentFiles")
    public AjaxResult getDocumentFiles() {
        AjaxResult result = AjaxResult.success();
        SysResource sysResource = sysResourceService.getSysResourceByName();
        String boatingNoticeFilename = sysResource.getBoatingNoticeFilename();
        if (!StringUtils.isEmpty(boatingNoticeFilename)) {
            URL boatingNoticeFileUrl = ossManager.generatePresignedUrl(boatingNoticeFilename);
            result.put("boatingNoticeFile", boatingNoticeFileUrl.toString());
        }
        String boatingGuideFilename = sysResource.getBoatingGuideFilename();
        if (!StringUtils.isEmpty(boatingGuideFilename)) {
            URL boatingGuideFileurl = ossManager.generatePresignedUrl(boatingGuideFilename);
            result.put("boatingGuideFile", boatingGuideFileurl.toString());
        }
        return result;
    }

    /**
     * 更新包船小图标。
     *
     * @param baochuanIconName
     * @return
     */
    @PostMapping("/updateBaochuanIcon")
    public AjaxResult updateBaochuanIcon(@RequestBody String baochuanIconName) {
        return toAjax(sysResourceService.updateBaochuanIcon(baochuanIconName));
    }

    /**
     * 删除包船小图标。
     *
     * @return
     */
    @DeleteMapping("/deleteBaochuanIcon")
    public AjaxResult deleteBaochuanIcon() {
        return toAjax(sysResourceService.deleteBaochuanIcon());
    }

    /**
     * 更新拼船小图标。
     *
     * @param pinchuanIconName
     * @return
     */
    @PostMapping("/updatePinchuanIcon")
    public AjaxResult updatePinchuanIcon(@RequestBody String pinchuanIconName) {
        return toAjax(sysResourceService.updatePinchuanIcon(pinchuanIconName));
    }

    /**
     * 删除拼船小图标。
     *
     * @return
     */
    @DeleteMapping("/deletePinchuanIcon")
    public AjaxResult deletePinchuanIcon() {
        return toAjax(sysResourceService.deletePinchuanIcon());
    }

    /**
     * 更新包船的描述文字。
     *
     * @param baochuanDescText
     * @return
     */
    @PostMapping("/updateBaochuanDescText")
    public AjaxResult updateBaochuanDescText(@RequestBody String baochuanDescText) {
        return toAjax(sysResourceService.updateBaochuanDescText(baochuanDescText));
    }

    /**
     * 更新拼船的描述文字。
     *
     * @param pinchuanDescText
     * @return
     */
    @PostMapping("/updatePinchuanDescText")
    public AjaxResult updatePinchuanDescText(@RequestBody String pinchuanDescText) {
        return toAjax(sysResourceService.updatePinchuanDescText(pinchuanDescText));
    }

    /**
     * 获取首页包船与拼船的描述文字及小图标。
     *
     * @return
     */
    @GetMapping("/getDescAndIcon")
    public AjaxResult getDescAndIcon() {
        SysResource sysResource = sysResourceService.getSysResourceByName();
        DescAndIcon descAndIcon = new DescAndIcon();
        BeanUtils.copyProperties(sysResource, descAndIcon);
        return success(descAndIcon);
    }

    /**
     * 更新隐私条款文件等相关文件。
     *
     * @return
     */
    @PostMapping("/updatePrivacyFiles")
    public AjaxResult updatePrivacyFiles(@RequestBody String privacyFileNames) {
        if (StringUtils.equals(privacyFileNames, "empty")) {
            return toAjax(sysResourceService.updatePrivacyFiles(""));
        } else {
            return toAjax(sysResourceService.updatePrivacyFiles(privacyFileNames));
        }

    }

}
